export enum Level {
  debug = 0,
  warn = 1,
  error = 2,
  none = 3
}
interface ILoggerOptions {
  prefix?: string;
  color?: string;
  bgColor?: string;
  level?: Level;
}
export default class Logger {
  private _level: Level = Level.debug;
  private _prefix: string = "logger";
  private _color: string = "#1e90ff";
  private _bgColor: string = "";
  constructor({ prefix, color, bgColor, level }: ILoggerOptions = {}) {
    if (level) this._level = level;
    if (color) this._color = color;
    if (bgColor) this._bgColor = bgColor;
    if (prefix) this._prefix = prefix;
    this.level(level);
  }
  private _getStyle(color: string, bgColor?: string): string {
    let style = `font-weight: bold;color:${color};`;
    if (bgColor) style += `background-color:${bgColor}`;
    return style;
  }
  public log(...args: any[]): void {
    if (this._level > Level.debug) return;
    console.log(
      `%c[${this._prefix}]`,
      this._getStyle(this._color, this._bgColor),
      ...args
    );
  }
  public warn(...args: any[]): void {
    if (this._level > Level.warn) return;
    console.warn(`%c[${this._prefix}]`, this._getStyle("#f39c12"), ...args);
  }
  public error(...args: any[]): void {
    if (this._level > Level.error) return;
    console.error(`%c[${this._prefix}]`, this._getStyle("#c0392b"), ...args);
  }
  public level(level = Level.warn): void {
    if (typeof level !== "undefined") this._level = level;
  }
}
